package com.eight.cloud.admin.controller;

import com.eight.cloud.admin.facade.SysTenantFacade;
import com.eight.cloud.admin.model.dto.TenantDto;
import com.eight.cloud.admin.model.dto.TenantSettledDto;
import com.eight.cloud.admin.service.ISysTenantService;
import com.eight.cloud.core.global.result.Result;
import com.eight.cloud.openfeign.admin.vo.TenantVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 租户管理
 *
 * @ClassName: SysTenantController
 * @Author: TXC
 * @Date: 2024-10-29 11:28
 **/
@RestController
@RequestMapping("/tenant")
public class SysTenantController {
    @Resource
    private SysTenantFacade sysTenantFacade;
    @Resource
    private ISysTenantService tenantService;
    /**
     * 租户入驻
     * @return @Link Result
     */
    @PostMapping("/settled")
    public Result<?> settled(@RequestBody TenantSettledDto settledVo) {
        boolean flag = sysTenantFacade.save(settledVo);
        if (!flag) {
            return Result.fail("入驻失败");
        }
        return Result.ok();
    }


    /**
     * 编辑
     * @param uuid 租户id
     * @param tenantDto 租户信息
     * @return @Link Result
     */
    @PutMapping("/{uuid}")
    public Result<?> edit(@PathVariable String uuid, @RequestParam TenantDto tenantDto) {
        boolean flag = tenantService.edit(uuid, tenantDto);
        if (!flag) {
            return Result.fail("编辑失败");
        }
        return Result.ok();
    }

    /**
     * 删除
     * @param uuid 租户id
     * @return @Link Result
     */
    @DeleteMapping("/{uuid}")
    public Result<?> delete(@PathVariable String uuid) {
        tenantService.removeById(uuid);
        return Result.ok();
    }

    /**
     * 查询租户详情
     * @param uuid 租户id
     * @return @Link Result
     */
    @GetMapping("/{uuid}")
    public Result<TenantVo> query(@PathVariable String uuid) {
        return Result.ok(tenantService.detail(uuid));
    }

    /**
     * 获取密钥
     * @param secretId 租户id
     * @return @Link Result
     */
    @GetMapping("/querySecret")
    public Result<TenantVo> queryTenantBySecretId(@RequestParam String secretId) {
        return Result.ok(tenantService.getTenantBySecretId(secretId));
    }

    /**
     * 生成密钥
     * @param uuid 租户id
     * @return
     */
    @GetMapping("/generateSecret/{uuid}")
    public Result<String> generateSecret(@PathVariable String uuid) {
        return Result.ok(tenantService.generateSecret(uuid));
    }

    /**
     * 更新密钥
     * @param uuid 租户id
     * @return
     */
    @GetMapping("/updateSecretKey/{uuid}")
    public Result<?> updateSecretKey(@PathVariable String uuid) {
        return Result.ok(tenantService.updateSecretKey(uuid));
    }
}
